package mwa.cs.util;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import mwa.cs.dao.SMApplicationLogDAO;
import mwa.cs.database.Database;
import mwa.cs.model.SMApplicationLog;

public class ApplicationLog extends HttpServlet {
	private static final long serialVersionUID = 1L;
 
    public ApplicationLog() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    public void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
    	request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/JSON");
		
    	JDate jDate = new JDate();
		String vaDate = jDate.getDate();
		String vaTime = jDate.getTime();
	
		Database db = new Database();
		HttpSession session = request.getSession();
		
		String vaMenuID = request.getParameter("menuID");
//		String vaMenuName = new String(request.getParameter("menuName").toString().getBytes("ISO-8859-1"),"UTF-8");
		String vaMenuName = request.getParameter("menuName").toString();
		String vaMenuURL = request.getParameter("menuURL");
		
		SMApplicationLog voLog = new SMApplicationLog(0, vaMenuID, JDate.getDate(), JDate.getTime(), 
				Integer.parseInt(session.getAttribute("vaUserId").toString()), vaMenuURL, vaMenuName, 
				session.getAttribute("vaURLAddress").toString().toString(), session.getAttribute("vaAgentBrowser").toString().toString());
		
		SMApplicationLogDAO voSMApplicationLog = new SMApplicationLogDAO(db);
		voSMApplicationLog.addLog(voLog);
		session.setAttribute("menuID", vaMenuID);
		response.sendRedirect("../"+vaMenuURL);
    }

    protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}


	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

}
